package org.dicker.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dicker.biz.BillBiz;
import org.dicker.biz.MessageBiz;
import org.dicker.biz.UserBiz;
import org.dicker.biz.impl.BillBizImpl;
import org.dicker.biz.impl.MessageBizImpl;
import org.dicker.biz.impl.UserBizImpl;
import org.dicker.entity.BillInfo;
import org.dicker.entity.BillSelector;
import org.dicker.entity.MessageInfo;
import org.dicker.entity.UserInfo;
import org.lanqiao.mvc.entity.ModelAndView;
import org.lanqiao.mvc.entity.YockMvcAnnotation.Controller;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestMapping;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestParam;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseBody;

import cn.hutool.json.JSONUtil;

@Controller("/billapi")
public class BillApi {

	private BillBiz biz=new BillBizImpl();
	private UserBiz userbiz=new UserBizImpl();
	
	/**
	* @catalog 账单模块
	* @title 查询用户账单列表
	* @method post
	* @url http://192.168.10.6:8080/big-birds/billapi/list_user.do
	* @param open_id 必选  String 用户oid
	* @param mintime 可选  最小日期,格式2020-10-10
	* @param maxtime 可选  最大日期,格式2020-10-10
	* @return_param array 账单列表
	*/
	@RequestMapping(value = "/list.do")
	@ResponseBody
	public String list(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "wx_openid")String open_id,
			@RequestParam(name = "mintime",defaultValue = "") String mintime,
			@RequestParam(name = "maxtime",defaultValue = "") String maxtime
			){
		try {
			req.setCharacterEncoding("utf-8");
			res.setCharacterEncoding("utf-8");
			res.setContentType("text/json; charset=UTF-8");
			
			BillSelector bs = new BillSelector();
			bs.setMintime(mintime);
			bs.setMaxtime(maxtime);
			List<BillInfo> list = biz.selectByUser(open_id,bs);
			
			String jsonStr = JSONUtil.toJsonStr(list);
			return jsonStr;
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	/**
	* @catalog 账单模块
	* @title 查询用户账单每个类型的资金统计列表
	* @method post
	* @url http://192.168.10.6:8080/big-birds/billapi/list_types.do
	* @param open_id 必选  String 用户oid
	* @param month 可选  月份
	* @param bill_type 可选 收支类型
	* @return_param array 账单列表
	*/
	@RequestMapping(value = "/list_types.do")
	@ResponseBody
	public String list_types(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "wx_openid")String open_id,
			@RequestParam(name = "month",defaultValue = "") String month,
			@RequestParam(name = "bill_type",defaultValue = "-1")int bill_type
			){
		try {
			
			req.setCharacterEncoding("utf-8");
			res.setCharacterEncoding("utf-8");
			res.setContentType("text/json; charset=UTF-8");
			
			BillSelector bs = new BillSelector();
			bs.setBill_type(bill_type);
			
			List<BillInfo> list = biz.selectByUserTypes(open_id,month,bs);
			
			String jsonStr = JSONUtil.toJsonStr(list);
			return jsonStr;
			
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	/**
	* @catalog 账单模块
	* @title 添加账单
	* @method post
	* @url http://192.168.10.6:8080/big-birds/billapi/list_user.do
	* @param open_id 必选  String 用户oid
	* @param bill_type 必选 收支类型
	* @param type_id 必选 类型
	* @param amount 必选 资金
	* @param create_time 必选 创建时间
	* @param remark 可选 备注
	* @return_param array 账单列表
	*/
	@RequestMapping(value = "/add.do")
	@ResponseBody
	public String add(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "wx_openid")String open_id,
			@RequestParam(name = "type_id")int type_id,
			@RequestParam(name = "amount")double amount,
			@RequestParam(name = "create_time")String create_time,
			@RequestParam(name = "remark",defaultValue = "")String remark
			){
		try {
			req.setCharacterEncoding("utf-8");
			res.setCharacterEncoding("utf-8");
			res.setContentType("text/json; charset=UTF-8");
			
			UserInfo user = userbiz.selectUserid(open_id);
			BillInfo bi = new BillInfo();
			bi.setUser_id(user.getUser_id());
			bi.setType_id(type_id);
			bi.setRemark(remark);
			bi.setAmount(amount);
			bi.setCreate_time(create_time);
			
			boolean isok = biz.add(bi);
			
			return isok?"1":"0";
			
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}
	
	
}
